package com.example.demo.service.impl;

import com.example.demo.entity.Permission;
import com.example.demo.entity.Role;
import com.example.demo.mapper.PermissionMapper;
import com.example.demo.service.PermissionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 权限服务实现类
 */
@Service
public class PermissionServiceImpl implements PermissionService {
    
    private static final Logger log = LoggerFactory.getLogger(PermissionServiceImpl.class);
    
    private final PermissionMapper permissionMapper;
    
    public PermissionServiceImpl(PermissionMapper permissionMapper) {
        this.permissionMapper = permissionMapper;
    }
    
    @Override
    public List<Role> getUserRoles(Long userId) {
        log.debug("查询用户{}的角色", userId);
        return permissionMapper.selectUserRoles(userId);
    }
    
    @Override
    public List<Permission> getUserPermissions(Long userId) {
        log.debug("查询用户{}的权限", userId);
        return permissionMapper.selectUserPermissions(userId);
    }
    
    @Override
    public List<String> getUserRoleCodes(Long userId) {
        log.debug("查询用户{}的角色编码", userId);
        return permissionMapper.selectUserRoleCodes(userId);
    }
    
    @Override
    public List<String> getUserPermissionCodes(Long userId) {
        log.debug("查询用户{}的权限编码", userId);
        return permissionMapper.selectUserPermissionCodes(userId);
    }
    
    @Override
    public boolean hasRole(Long userId, String roleCode) {
        int count = permissionMapper.countUserRole(userId, roleCode);
        boolean hasRole = count > 0;
        log.debug("用户{}是否有角色{}: {}", userId, roleCode, hasRole);
        return hasRole;
    }
    
    @Override
    public boolean hasPermission(Long userId, String permissionCode) {
        int count = permissionMapper.countUserPermission(userId, permissionCode);
        boolean hasPermission = count > 0;
        log.debug("用户{}是否有权限{}: {}", userId, permissionCode, hasPermission);
        return hasPermission;
    }
}
